package com.yunding.util;

import com.yunding.exception.SystemException;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.InvocationTargetException;

/**
 * <p>
 *
 * </p>
 *
 * @author 杜奕明
 * @date 2020/1/18
 */

// TODO: 2020/8/30  
public class DtoUtil {

    public static <E, D> E toEntity(D dto, Class<E> entityType) {
        E entity;
        try {
            entity = entityType.getDeclaredConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new SystemException(e);
        }
        BeanUtils.copyProperties(dto, entity);
        return entity;
    }
}
